package com.isoftstone.project.system.dict.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.isoftstone.project.system.dict.domain.Dict;
import com.isoftstone.project.system.dict.service.IDictService;
import com.isoftstone.framework.controller.BaseController;
import com.isoftstone.framework.domain.Page;
import com.isoftstone.framework.domain.Tidings;
import com.isoftstone.common.utils.ExcelUtil;
import com.isoftstone.framework.domain.PageData;

/**
 * 数据字典Controller
 *
 * @author guoX
 * @date 2021-02-01
 */
@Controller
@RequestMapping("/system/dict")
public class DictController extends BaseController {
    private String prefix = "system/dict";

    @Autowired
    private IDictService dictService;

    @RequiresPermissions("system:dict:view")
    @GetMapping()
    public String dict() {
        return prefix + "/dict_list";
    }

    /**
     * 查询数据字典列表
     */
    @RequiresPermissions("system:dict:list")
    @PostMapping("/dictlistPage")
    @ResponseBody
    public Object getdictlistPage(Page page) {
        PageData pd = this.getPageData();
        page.setPd(pd);
        List<Dict> list = dictService.getDictlistPage(page);
        return Tidings.success(page.getTotalResult(), list);
    }


    /**
     * 导出数据字典列表
     */
    @RequiresPermissions("system:dict:export")
    @PostMapping("/exportDict")
    @ResponseBody
    public Object exportDict(Page page) {
        PageData pd = this.getPageData();
        page.setPd(pd);
        page.setEntityOrField(false);
        List<Dict> list = dictService.getDictlistPage(page);
        ExcelUtil<Dict> util = new ExcelUtil<Dict>(Dict. class);
        return util.exportExcel(list, "dict");
    }

    /**
     * 新增数据字典
     */
    @GetMapping("/addDict")
    public String addDict() {
        return prefix + "/dict_add";
    }


    /**
     * 新增保存数据字典
     */
    @RequiresPermissions("system:dict:add")
    @PostMapping("/addDict")
    @ResponseBody
    public Object insertDict(Dict dict) {
        dictService.insertDict(dict);
        return Tidings.success();

    }

    /**
     * 修改数据字典
     */
    @GetMapping("/toEditDict")
    public String toEditDict(String dictid, ModelMap mmap) {
        Dict dict =dictService.getDictById(dictid);
        mmap.put("dict", dict);
        return prefix + "/dict_edit";
    }

    /**
     * 修改保存数据字典
     */
    @RequiresPermissions("system:dict:edit")
    @PostMapping("/updateDict")
    @ResponseBody
    public Object updateDict(Dict dict) {
        dictService.updateDict(dict);
        return Tidings.success();
    }

    /**
     * 删除数据字典
     */
    @RequiresPermissions("system:dict:remove")
    @PostMapping("/deleteDict")
    @ResponseBody
    public Object deleteDict(String dictid) {
        dictService.deleteDictByIds(dictid);
        return Tidings.success();
    }

}
